package com.czc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/*
    Security配置类
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Bean
    public PasswordEncoder getpw(){
        return new BCryptPasswordEncoder();
    }

    /*
        自定义登录页面
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()//表示表单提交
                .loginPage("/login.html")//自定义登录页面
                //必须和表单提交的借口一样,会去执行自定义登录逻辑
                .loginProcessingUrl("/login")
                //登录成功后跳转到的页面
                .successForwardUrl("/toMain");


        //授权
        http.authorizeRequests()
                //放行/login.html  不需要认证
                .antMatchers("/login.html").permitAll()
                .anyRequest().authenticated();//所有请求都必须认证才能访问,必须登录才能访问

        //关闭csrf
        http.csrf().disable();
    }
}
